//
//  RNOLSelectCouponControllerViewController.m
//  RongNiuOnline
//
//  Created by apple on 2018/5/21.
//  Copyright © 2018年 rongniu. All rights reserved.
//

#import "RNOLSelectCouponControllerViewController.h"
#import "RNOLDismissAnimationTransition.h"
#import "RNOLUseCouponTopBar.h"
#import "RNOLSelectCouponSubViewController.h"

@interface RNOLSelectCouponControllerViewController () <UIViewControllerTransitioningDelegate>

@property (nonatomic,strong) RNOLDismissAnimationTransition * dismissAnimation;
@property (nonatomic,strong) RNOLUseCouponTopBar *topBar;
@property (nonatomic,strong) RNOLSelectCouponSubViewController *redBagVC;
@property (nonatomic,strong) RNOLSelectCouponSubViewController *couponVC;
@property (nonatomic,strong) id  obj;

@end

@implementation RNOLSelectCouponControllerViewController

- (RNOLUseCouponTopBar *)topBar{
    if (!_topBar) {
        _topBar = [[RNOLUseCouponTopBar alloc] initWithFrame:CGRectMake(0, 0, KWIDTH , kTopBarViewHeight)];
    }
    return _topBar;
}

- (RNOLDismissAnimationTransition *)dismissAnimation{
    if (!_dismissAnimation) {
        _dismissAnimation = [[RNOLDismissAnimationTransition alloc] init];
    }
    return _dismissAnimation;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initUISetting];
    [self doSomethingsEventsAction];
}


- (void)initUISetting{
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.minYPullUp = kTopBarViewHeight;
    self.maxYPullDown = kTopBarViewHeight;
    self.view.backgroundColor = KWhiteColor;
    [self.view addSubview: self.topBar];
    
    UIView*bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, KHIGHT*0.7 - kTopBarViewHeight, KWIDTH , kTopBarViewHeight)];
    bottomView.backgroundColor = [UIColor whiteColor];
    UILabel *label = [RNOLUnitTool createLabelWithText:@"注: 红包和礼券不能同时使用。" textColor: [UIColor redColor] fontSize:14];
    label.frame = CGRectMake(15, 0, KWIDTH - 30 , 21);
    label.centerY = bottomView.height/2.0;
    [bottomView addSubview: label];
    [self.view addSubview: bottomView];
    [self reloadData];
}

- (void)doSomethingsEventsAction{
    @weakify(self);
    [self.topBar setCloseBlock:^{
       @strongify(self);
        [self dismissAction];
    }];
    
    //确认
    [self.topBar setConfirmBlock:^{
       @strongify(self);
        !self.confirmBlock? : self.confirmBlock();
        [self dismissAction];
        
    }];
}


#pragma mark- dismiss
- (void)dismissAction{
    self.transitioningDelegate = self;
    [self dismissViewControllerAnimated:YES completion:^{

    }];
}

#pragma mark - UIViewControllerTransitioningDelegate
- (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed{
    return self.dismissAnimation;
}


/**
 控制器继承 SPCoverController
 此方法返回的是标题栏 标题
 */
- (NSString *)titleForIndex:(NSInteger)index
{
    return @[@"使用红包",@"使用加息券"][index];
}


- (CGFloat)tabWidthForIndex:(NSInteger)index{
    return KWIDTH/2.0;
}

- (CGFloat)markViewWidthForIndex:(NSInteger)index{
    return KWIDTH/3.0;
}

/**
 下面联动的ChildController控制器的数量
 */
- (NSInteger)numberOfControllers
{
    return 2;
}

- (CoverScrollStyle)preferCoverStyle{
    return CoverScrollStyleTop;
}

- (CGFloat)preferTabY{
    return kTopBarViewHeight;
}


/**
 下面联动的ChildController控制器
 */
- (UIViewController *)controllerAtIndex:(NSInteger)index
{
    RNOLSelectCouponSubViewController *redBagVC = [RNOLSelectCouponSubViewController new];
    redBagVC.isRedBag = YES;
    self.redBagVC = redBagVC;

    RNOLSelectCouponSubViewController *couponVC = [RNOLSelectCouponSubViewController new];
    couponVC.isRedBag = NO;
    self.couponVC = couponVC;
    
    return @[redBagVC,couponVC][index];
}

//页面切换已在SPTabcontroller 实现
- (void)didPressTabForIndex:(NSInteger)index{
    [super didPressTabForIndex: index];

}

//横向滑动
- (void)scrollViewContentOffsetWithRatio:(CGFloat)ratio draging:(BOOL)draging{
    [super scrollViewContentOffsetWithRatio:ratio draging:draging];
    
}


@end
